home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / tvtool17.zip / PE112.H < prev    next >
Text File  |  1993-07-16  |  12KB  |  516 lines

  1. /*-
  2.  
  3.   PE112 Library: definitions
  4.  
  5.   Author:    Marc Vauclair
  6.   Date:        July 3, 1993
  7.   Revised:     July 13, 1993
  8.         (The MS-DOS modifications are authored by Marc Stern)
  9.  
  10.   RCS Information:
  11.   ---------------
  12.   $Author: vauclair $
  13.   $Date: 1993/07/15 08:16:15 $
  14.   $Revision: 1.9 $
  15.   $RCSfile: PE112.h,v $
  16.   $Source: /u/vauclair2/dd/PE112/RCS/PE112.h,v $
  17.   $State: Exp $
  18.   $Locker: vauclair $
  19.  
  20.   Notes:
  21.   -----
  22.   1) if compiled under Unix, the UNIX symbol must be defined
  23.  
  24.   2) if compiled under MS-DOS, the MSDOS symbol must be defined
  25.  
  26.   3) This file is still to be restructured and extensively documented.
  27.  
  28. -*/
  29.  
  30. #if !defined(___PE112_h)
  31.  
  32. #define ___PE112_h
  33.  
  34. #if ! defined(UNIX) && ! defined(MSDOS)
  35. /* The #error directive is not accepted by the Sun C compiler */
  36. /*#error You must define MSDOS or UNIX */
  37. #endif
  38.  
  39. /*-
  40.   The traditional includes.
  41.   They are scattered throughout the various .c files to speed up
  42.   the compilation and earn disk space with precompiled header files.
  43. -*/
  44.  
  45. #if defined(DEBUG)
  46. # include <stdio.h>
  47. #endif
  48.  
  49.  
  50.  
  51. /*-
  52.   What follow is a bit special. To guarantee the portability of the code
  53.   one has to make sure that all the symbols to be manipulated by the linker
  54.   are unique in the first 6 characters. This means that with our convention
  55.   of prefixing all names with PE112_ the 6 characters are already eaten and
  56.   that for a dumb linker they are all alike.
  57.  
  58.   This is the reason why we define here a six letters equivalent symbol
  59.   for all the EXTERN or static functions and for all the global variables.
  60.  
  61.   The disadvantage of this method is that if one has to resort to a symbolic
  62.   debugger he won't see the API names but rather the nonsense SC__xx names.
  63.   This is a no problem because the entire code of the library is written
  64.   with a defensive programming approach that would (hopefully ;-) make void
  65.   the need for a symbolic debugger.
  66.  
  67.   One advantage of this method is that the symbol table is scrambled: someone
  68.   having access to it can't infer the use of the functions from their names.
  69. -*/
  70.  
  71. #define PE112_sys_open        SC__01
  72. #define PE112_sys_close        SC__02
  73. #define PE112_sys_read        SC__03
  74. #define PE112_sys_write        SC__04
  75.  
  76. #define PE112_debug        SC__08
  77.  
  78. #define PE112_reply_names    SC__09
  79.  
  80. #define PE112_set_lock_duration SC__10
  81. #define PE112_open_session    SC__11
  82. #define PE112_close_session    SC__12
  83. #define PE112_process_command    SC__13
  84.  
  85. #define PE112_reset        SC__18
  86. #define PE112_swallow        SC__19
  87. #define PE112_eject        SC__20
  88. #define PE112_reader_identification_request    SC__21
  89. #define PE112_reader_status_request        SC__22
  90. #define PE112_get_mask        SC__23
  91. #define PE112_get_config    SC__24
  92. #define PE112_card_power_on    SC__25
  93. #define PE112_card_power_off    SC__26
  94.  
  95.  
  96.  
  97. /* convenient types */
  98. typedef unsigned int UINT;
  99. typedef unsigned char BYTE;
  100.  
  101. #define PRIVATE    static
  102. #define PUBLIC
  103.  
  104. #define FPRINTF    (void)fprintf
  105. #define FFLUSH    (void)fflush
  106.  
  107. /* INFINITE_TIMEOUT is the no timeout value */
  108. #define INFINITE_TIMEOUT -1L
  109.  
  110. #define FAILURE    -1
  111.  
  112. #if !defined(TRUE)
  113. # define TRUE 1
  114. /* pedantic: # define TRUE (1==1) */
  115. #endif
  116.  
  117. #if !defined(FALSE)
  118. #define FALSE 0
  119. /* pedantic: # define FALSE (0==1) */
  120. #endif
  121.  
  122. /* note the "/" at the end, it is mandatory */
  123. /* this is the name of the directory where the locks are kept,
  124.    it must be readable, writeable and searchable by everybody */
  125. #if !defined(PE112_lock_directory)
  126.  
  127. #if defined(UNIX)
  128. # define PE112_lock_directory    "/tmp/"
  129. #endif
  130.  
  131. #if defined(MSDOS)
  132. # define PE112_lock_directory    "c:\\"
  133. #endif
  134.  
  135. #endif !defined(PE112_lock_directory)
  136.  
  137. #if defined(UNIX)
  138. # define PE112_library_id    "PE112_"
  139. #endif defined(UNIX)
  140.  
  141. #if defined(MSDOS)
  142. /* - those poor MSDOS users are still limited to 8 chars for the principal
  143.      part of a file name :-(
  144.    - device name = COMx
  145.  */
  146. # define PE112_library_id    "PE_"
  147. #endif defined(MSDOS)
  148.  
  149.  
  150. #define PE112_magic_number    0x15
  151. #define PE112_packet_length    256
  152.  
  153. #if !defined(PE112_default_lock_duration)
  154. # define PE112_default_lock_duration 120
  155. #endif
  156.  
  157. #if defined(HAVE_PROTOTYPES)
  158. #define PROTO(name, args)  name args
  159. #else
  160. #define PROTO(name, args)  name()
  161. #endif
  162.  
  163. #ifndef EXTERN
  164.  
  165. # if defined(__cplusplus) || defined(__cplusplus__)
  166. #  define EXTERN   extern "C"
  167. # else
  168. #  define EXTERN   extern 
  169. # endif
  170.  
  171. #endif
  172.  
  173.  
  174.  
  175. /* Debugging macros and declarations */
  176.  
  177. /*- 
  178.   The tracing is controlled by two things: a #defined symbol DEBUG and
  179.   a global variable PE112_debug. The DEBUG symbol can be turned on
  180.   on the compilation command line in the makefile.
  181.  
  182.   If DEBUG is not defined, there is not tracing at all and all the tracing
  183.   code is not compiled in.
  184.  
  185.   If DEBUG is defined, the tracing code is compiled in and a global
  186.   variable PE112_debug controls the selection of the messages to be
  187.   printed out. This approach allows the application programmer to
  188.   details to be traced and also to control dynamically the sections 
  189.   of his code where he wants the tracing turned on or off.
  190.  
  191.   PE112_debug takes as value an OR combination of the DEBUG_xxx values.
  192.   A special DEBUG_NONE value is given to turn dynamically all the tracing
  193.   off.
  194.  
  195.   The application program modifies dynamically the tracing details by
  196.   modifying the content of the PE112_debug variable. It is the 
  197.   responsability of the application program to make sure that 
  198.   these operations are only performed if the DEBUG symbol is defined.
  199. -*/
  200.  
  201. /*-
  202.   The dprint is a special macro in the sense that it is one of the
  203.   possible implementations of a macro with a variable number of
  204.   arguments.
  205.  
  206.   When used, args MUST be put within (). E.g.:
  207.  
  208.       dprint(DEBUG_FLAG, ("Value: %d, Label: %s\n", value, label));
  209. -*/
  210.  
  211. #define DEBUG_NONE    0        /* no tracing at all */
  212. #define DEBUG_SYS    (1<<0)        /* System layer      */
  213. #define DEBUG_0LAYER    (1<<1)        /* Layer 0 main      */
  214. #define DEBUG_0AUX    (1<<2)        /* Layer 0 auxiliary */
  215. #define DEBUG_1LAYER    (1<<3)        /* Layer 1           */
  216.  
  217. #if defined(DEBUG)
  218.  
  219.     EXTERN int PE112_debug;
  220.  
  221. #    define dprint(flag, args) \
  222.           if (PE112_debug & (flag)) { \
  223.           (void)printf("flag: "); \
  224.           (void)printf args; \
  225.           (void)fflush(stdout); \
  226.             }
  227. #    define dprint2(flag, args) \
  228.           if (PE112_debug & (flag)) { \
  229.           (void)printf args; \
  230.           (void)fflush(stdout); \
  231.             }
  232. #    define dperror(flag, val, mes) \
  233.           if (PE112_debug & (flag)) { \
  234.           if ((val) < 0) { \
  235.            (void)printf("flag: "); \
  236.            (void)fflush(stdout); \
  237.            perror(mes); \
  238.            (void)fflush(stderr); \
  239.         } \
  240.         }
  241. #else !defined(DEBUG)
  242.  
  243. #    define dprint(flag, args)
  244. #    define dprint2(flag, args)
  245. #    define dperror(flag, val, mes)
  246.  
  247. #endif !defined(DEBUG)
  248.  
  249.  
  250.  
  251. typedef struct {
  252.   UINT length;
  253.   BYTE buf[PE112_packet_length];
  254. } PE112_packet;
  255.  
  256. /*-
  257.   The PE112_status_code enumerates the possible values returned by
  258.   Layer 0 functions within the PE112_session_info data.
  259. -*/
  260.  
  261. typedef enum { 
  262.   /* success code */
  263.   PE112_SUCCESS = 0,
  264.  
  265.   /* generic failure code (used mainly for initialisations) */
  266.   PE112_FAILURE,
  267.  
  268.   /* Error codes */
  269.   PE112_TTY_OPEN_FAILURE,
  270.   PE112_TTY_OPEN_TIMEOUT,
  271.   PE112_TTY_GET_TERMIOS_FAILURE,
  272.   PE112_TTY_SET_INPUT_SPEED_FAILURE,
  273.   PE112_TTY_SET_OUTPUT_SPEED_FAILURE,
  274.   PE112_TTY_SET_TERMIOS_FAILURE,
  275.  
  276.   PE112_FASTNET_HEADER_WRITE_ERROR,
  277.   PE112_WRITE_COMMAND_ERROR,
  278.   PE112_FASTNET_HEADER_READ_ERROR,
  279.   PE112_READ_ANSWER_ERROR,
  280.  
  281.   PE112_READER_RESET_FAILURE,
  282.  
  283.   PE112_IN_USE,
  284.  
  285.   PE112_DEVICE_CLOSE_FAILURE,
  286.   PE112_UNLOCK_FAILURE
  287.  
  288. } PE112_status_code;
  289.  
  290. typedef struct {
  291.   PE112_status_code status;
  292.   char * devname;
  293.   char * lockname;
  294.   int fd;
  295. } PE112_session_info;
  296.  
  297. /*-
  298.   The PE112_reply_code enumerates the possible values for the interpretation
  299.   of the Smart Card reader answer packets.
  300.  
  301.   These values are not to be confused with those defined by PE112_status_code.
  302. -*/
  303.  
  304. typedef enum {
  305.  
  306.   RESET_UNKNOWN, 
  307.   RESET_WRONG_LENGTH, 
  308.   RESET_FAIL,
  309.   RESET_OK,
  310.  
  311.   SWALLOW_UNKNOWN, 
  312.   SWALLOW_WRONG_LENGTH, 
  313.   SWALLOW_FAIL, 
  314.   SWALLOW_OK,
  315.   SWALLOW_SHORT, 
  316.   SWALLOW_JAM, 
  317.   SWALLOW_UNKNOWN_REPLY